package com.huayun.plugin.fastener.controller;

import com.huayun.plugin.fastener.service.FileParser;
import com.huayun.plugin.fastener.utils.LinePointCalculator;
import com.huayun.plugin.fastener.utils.QuarticEquationSolver;
import com.huayun.plugin.fastener.vo.MatrixRequest;
import com.huayun.plugin.fastener.vo.PointRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.huayun.plugin.fastener.utils.CoordinateTransform.*;

@RestController
@RequestMapping("fastener")
public class FastenerController {

    @Autowired
    private FileParser fileParser;

    @RequestMapping(value = "/calculate", method = RequestMethod.POST)
    @ResponseBody
    public List<Double> calculate(@RequestBody List<Double> nums) {
        System.out.println(nums);
        double a = nums.get(0), b = nums.get(1), c = nums.get(2), d = nums.get(3), e = nums.get(4);
        List<Double> roots = QuarticEquationSolver.solveQuartic(a, b, c, d, e);
        System.out.println("方程的根为：");
        for (double root : roots) {
            System.out.println(root);
        }
        return roots;
    }

    @RequestMapping(value = "/transformMatrix", method = RequestMethod.POST)
    @ResponseBody
    public List<Double> transformMatrix(@RequestBody MatrixRequest request) {
        double[] partStart = request.getPartStart();
        double[] partEnd = request.getPartEnd();
        double[] assemblyStart = request.getAssemblyStart();
        double[] assemblyEnd = request.getAssemblyEnd();
        List<Double> matrix1 = calculateTransformMatrix(partStart, partEnd, assemblyStart, assemblyEnd);
        List<Double> matrix2 = calculateTransformMatrix2(partStart, partEnd, assemblyStart, assemblyEnd);

        return matrix1;
    }
    @RequestMapping(value = "/transformMatrix2", method = RequestMethod.POST)
    @ResponseBody
    public List<Double> transformMatrix2(@RequestBody MatrixRequest request) {
        double[] partStart = request.getPartStart();
        double[] partEnd = request.getPartEnd();
        double[] assemblyStart = request.getAssemblyStart();
        double[] assemblyEnd = request.getAssemblyEnd();
        List<Double> matrix = calculateTransformMatrix2(partStart, partEnd, assemblyStart, assemblyEnd);

        return matrix;
    }

    @RequestMapping(value = "/transformMatrixFromThreePoints", method = RequestMethod.POST)
    @ResponseBody
    public List<Double> transformMatrix1(@RequestBody MatrixRequest request) {
        double[] partStart = request.getPartStart();
        double[] partEnd = request.getPartEnd();
        double[] partCenter= request.getPartCenter();
        double[] assemblyStart = request.getAssemblyStart();
        double[] assemblyEnd = request.getAssemblyEnd();
        double[] assemblyCenter = request.getAssemblyCenter();
        return calculateTransformMatrixThreePoints(partStart, partEnd, partCenter, assemblyStart, assemblyEnd, assemblyCenter);
    }

    @RequestMapping(value = "/calculateNewPoint", method = RequestMethod.POST)
    @ResponseBody
    public double[] calculateNewPoint(@RequestBody PointRequest request) {

        double[] onePoint= request.getOnePoint();
        double[] twoPoint= request.getTwoPoint();
        double[] threePoint= request.getThreePoint();
        double distance= request.getDistance();

        return LinePointCalculator.calculateNewPoint(onePoint, twoPoint, threePoint, distance);
    }

    @RequestMapping(value = "/calculateDistance", method = RequestMethod.POST)
    @ResponseBody
    public double calculateDistance(@RequestBody PointRequest request) {
        double[] onePoint= request.getOnePoint();
        double[] twoPoint= request.getTwoPoint();
        return LinePointCalculator.calculateDistance(onePoint, twoPoint);
    }

    @PostMapping("/uploadXml")
    public ResponseEntity<String> uploadXmlFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("Please select a file to upload.");
        }
        try {
            String result = fileParser.parseXml(file);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to parse XML file: " + e.getMessage());
        }
    }



}
